package com.example.webupload.controller;

import com.example.webupload.common.Result;
import com.example.webupload.dto.ChunkMergeDTO;
import com.example.webupload.dto.ChunkUploadDTO;
import com.example.webupload.dto.FileUploadDTO;
import com.example.webupload.service.FileService;
import com.example.webupload.vo.FileInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文件上传下载控制器
 */
@Slf4j
@Api(tags = "文件管理接口")
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public Result<FileInfoVO> uploadFile(@Validated FileUploadDTO fileUploadDTO, HttpServletRequest request) {
        FileInfoVO fileInfo = fileService.uploadFile(fileUploadDTO, request);
        return Result.success(fileInfo);
    }

    @ApiOperation("上传文件分片")
    @PostMapping("/uploadChunk")
    public Result<Boolean> uploadChunk(@Validated ChunkUploadDTO chunkUploadDTO, HttpServletRequest request) {
        boolean success = fileService.uploadChunk(chunkUploadDTO, request);
        return Result.success(success);
    }

    @ApiOperation("合并文件分片")
    @PostMapping("/mergeChunks")
    public Result<FileInfoVO> mergeChunks(@Validated @RequestBody ChunkMergeDTO chunkMergeDTO, HttpServletRequest request) {
        FileInfoVO fileInfo = fileService.mergeChunks(chunkMergeDTO, request);
        return Result.success(fileInfo);
    }

    @ApiOperation("检查文件是否已存在（秒传）")
    @GetMapping("/check")
    public Result<FileInfoVO> checkFileExists(@RequestParam String md5) {
        FileInfoVO fileInfo = fileService.checkFileMd5(md5);
        return Result.success(fileInfo);
    }

    @ApiOperation("获取文件信息")
    @GetMapping("/{id}")
    public Result<FileInfoVO> getFileInfo(@PathVariable Long id) {
        FileInfoVO fileInfo = fileService.getFileInfo(id);
        return Result.success(fileInfo);
    }

    @ApiOperation("获取文件列表")
    @GetMapping("/list")
    public Result<List<FileInfoVO>> getFileList() {
        List<FileInfoVO> fileList = fileService.getFileList();
        return Result.success(fileList);
    }

    @ApiOperation("下载文件")
    @GetMapping("/download/{id}")
    public void downloadFile(@PathVariable Long id, HttpServletResponse response) {
        fileService.downloadFile(id, response);
    }

    @ApiOperation("预览文件")
    @GetMapping("/preview/{id}")
    public void previewFile(@PathVariable Long id, HttpServletResponse response) {
        fileService.previewFile(id, response);
    }

    @ApiOperation("删除文件")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteFile(@PathVariable Long id) {
        boolean success = fileService.deleteFile(id);
        return Result.success(success);
    }
}